<template>
	<view class="container">
		<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" collection="uni-id-users"
			field="username,station,gender,mobile,email" loadtime="onready" :getone="true" :where="queryWhere">
			<view v-if="error">{{error.message}}</view>
			<view v-else-if="loading">
				<uni-load-more :contentText="loadMore" status="loading"></uni-load-more>
			</view>
			<view v-else-if="data">
				<view class="base-info">
					<view class="base-info-item">
						<view class="base-info-item-label">员工号:</view>
						<view>{{data._id}}</view>
					</view>
					<view class="base-info-item">
						<view class="base-info-item-label">员工姓名:</view>
						<view>{{data.username}}</view>
					</view>
					<view class="base-info-item">
						<view class="base-info-item-label">员工岗位:</view>
						<view>{{data.station}}</view>
					</view>
					<view class="base-info-item">
						<view class="base-info-item-label">员工性别:</view>
						<view>{{data.gender | transformGender}}</view>
					</view>
					<view class="base-info-item">
						<view class="base-info-item-label">员工手机:</view>
						<view>{{data.mobile}}</view>
					</view>
					<view class="base-info-item">
						<view class="base-info-item-label">员工邮箱:</view>
						<view>{{data.email}}</view>
					</view>
				</view>
				<view class="holiday-info">
					<view class="base-info-item">
						<view class="base-info-item-label">加班时长:</view>
						<view>{{holidayInfo.overtimeWork}}天</view>
					</view>
					<view class="base-info-item">
						<view class="base-info-item-label">已使用调休假:</view>
						<view>{{holidayInfo.holiday}}天</view>
					</view>
					<view class="base-info-item">
						<view class="base-info-item-label">剩余调休假:</view>
						<view>{{holidayInfo.overtimeWork - holidayInfo.holiday}}天</view>
					</view>
				</view>
			</view>
		</unicloud-db>
		<view class="btns">
			<button type="primary" @click="handleUpdate">修改</button>
			<button style="color: white;background-color: orange;" class="btn-delete" @click="resetPassword">重置</button>
			<button type="warn" class="btn-delete" @click="handleDelete">删除</button>
		</view>
		<button type="primary" @click="jumpOLview">调休假情况</button>
		<!-- <button class="holiday-btn" type="default" @click="addHoliday">加班录入</button> -->
	</view>
</template>

<script>
	const db = uniCloud.database();
	const $ = db.command.aggregate;
	export default {
		data() {
			return {
				holidayInfo: { // 调休假相关信息
					holiday: 0, // 调休天数
					overtimeWork: 0 // 加班天数
				},
				queryWhere: '',
				loadMore: {
					contentdown: '',
					contentrefresh: '',
					contentnomore: ''
				}
			}
		},
		filters: {
			// 性别数字转文字
			transformGender(type) {
				switch (type) {
					case 0:
						return '未知';
					case 1:
						return '男';
					case 2:
						return '女';
					default:
						return '未知';
				}
			}
		},
		onLoad(e) {
			this._id = e.id;
		},
		onReady() {
			if (this._id) {
				this.queryWhere = '_id=="' + this._id + '"';
				this.getHolidayInfo();
			}
		},
		methods: {
			jumpOLview(){
				//查看员工调休假详情
				uni.navigateTo({
					url:'../overtime/adminOtList?id=' + this._id ,
					fail(e) {
						console.log(e);
					}
				})
			},
			// 获取调休假信息
			getHolidayInfo() {
				let that = this;
				let overtimeWork = 0;
				let holiday = 0;
				this.request('business/work/getWorkReleaxLength', {}).then(res => {
					if (res.code === 0) {
						that.holidayInfo.holiday = res.data.releaxTotal;
						that.holidayInfo.overtimeWork = res.data.workTotal;
					} else {
						uni.showToast({
							icon: 'none',
							title: '网络错误，请稍后重试'
						})
					}
				}).catch(err => {
					uni.showToast({
						icon: 'none',
						title: '网络错误，请稍后重试'
					})
				})
			},
			handleUpdate() {
				// 打开修改页面
				uni.navigateTo({
					url: './edit?id=' + this._id,
					events: {
						// 监听修改页面成功修改数据后, 刷新当前页面数据
						refreshData: () => {
							this.$refs.udb.loadData({
								clear: true
							})
						}
					}
				})
			},
			//加班录入
			addHoliday() {

			},
			handleDelete() {
				this.$refs.udb.remove(this._id, {
					success: (res) => {
						// 删除数据成功后跳转到list页面 
						uni.navigateBack({
							delta: 1
						});
					}
				})
			},
			// 重置密码
			resetPassword() {
				let that = this;
				uni.showModal({
					title: '重置密码',
					content: '是否确认重置密码',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							that.request('common/user/resetPwd', {}).then(res => {
								if (res.code == 0) {
									uni.showToast({
										title: '重置密码成功',
										icon:"none",
										duration: 2000
									});
								} else {
									uni.showToast({
										title: '重置密码失败,请联系管理员',
										icon:"none",
										duration: 2000
									});
								}
							}).catch().finally(() => {
								uni.hideLoading()
							})

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}
		}
	}
</script>

<style>
	.holiday-info,
	.base-info {
		margin: 10px;
		padding: 10px;
		border-radius: 8px;
		border: 1px solid #8F8F94;
	}

	.base-info-item {
		padding: 10rpx;
		display: flex;
		align-items: center;
	}

	.base-info-item-label {
		width: 32%;
	}

	.holiday-btn {
		margin: 20rpx;
	}

	.container {
		padding: 10px;
	}

	.btns {
		margin: 10px 0px;
		display: flex;
		flex-direction: row;
	}

	.btns button {
		flex: 1;
	}

	.btn-delete {
		margin-left: 10px;
	}
</style>
